//$fa=2;
//$fs=0.2;

use <stepper.scad>
use <rubik_cube.scad>
use <claw.scad>


thinkness = 10;
width     = 42;
offset_form_cube = 73;// 魔方中心到步进电机端面的距离
stepper_length = 55;  // 步进电机长度
m3_hole_through = 3.4;// m3通孔
m4_hole_through = 4.6;// m4通孔

// 14x6mm光电开关
module itr9606(){
    difference(){
        translate([-7,-3,0]) cube([14,6,10]);
        translate([-2.5,-5,-2.5]) cube([5,10,10]);
        translate([-3,-0.25,-2.5]) cube([6,0.5,10]);
    }
}
module itr9606_hole(){
    gap = 0.6;
    difference(){
        translate([-7-gap/2,-3-gap/2,0]) cube([14+gap,6+gap,20]);
        translate([-2.5+gap/2,-5,-2.5]) cube([5-gap,10,10]);
    }
}

// 台阶孔
module m3_hole_diff()
{
    union(){
        cylinder(h=40, d=m3_hole_through, center=true);
        translate([0,0,7])cylinder(h=40, d=6);
    }
}
module d_eq_thinkness_cylinder(a=42)
{
    rotate([0,90,0]) cylinder(a,d=thinkness,center=true);
}
module stepper_holder(show_itr9606)
{
    length = 52;
    if(show_itr9606){
        // 1mm 4.5mm
        translate([0, -41, 16.5]) rotate([180,0,90]) 
            color("grey") itr9606();
    }
    union(){
        // 电机固定板
        difference(){
            translate([0,-(length-width)/2, thinkness/2]) cube([width,length,thinkness], center=true);
            translate([31/2, 31/2, 0])  m3_hole_diff();
            translate([-31/2, 31/2, 0]) m3_hole_diff();
            translate([31/2, -31/2, 0]) m3_hole_diff();
            translate([-31/2, -31/2, 0])m3_hole_diff();
            // 电机轴外围的22mm x 2mm圆台
            translate([0, 0, -10+2.5]) cylinder(h=10,d=22+1);
            // 电机轴
            cylinder(h=40,d=10,center=true);
        }
        // 三角形支架
        difference(){
            x1 = -width/2 - thinkness/2-0.5;
            x2 = x1 - stepper_length;
            y1 = thinkness/2;
            y2 = y1 - stepper_length;
            hull(){
                translate([0, x1, y2]) d_eq_thinkness_cylinder(width);
                translate([0, x1, y1]) d_eq_thinkness_cylinder(width);
                translate([0, x2 ,y1]) d_eq_thinkness_cylinder(width);
            }
            hull(){
                translate([0, x1-thinkness, y2+(1+sqrt(2))*thinkness]) 
                    d_eq_thinkness_cylinder(60);
                translate([0, x1-thinkness, y1-thinkness]) 
                    d_eq_thinkness_cylinder(60);
                translate([0, x2+(1+sqrt(2))*thinkness ,y1-thinkness]) 
                    d_eq_thinkness_cylinder(60);
            }
            translate([0, -41, 16.5]) rotate([180,0,90]) 
                itr9606_hole();
        }
    }
}
//stepper_holder(58);

module base(show_itr9606){
    difference(){
        union(){
            rotate([-135,0,0]) {
                translate([0,0,offset_form_cube]) rotate([0,180,180]) 
                    stepper_holder(show_itr9606);
            }   
            rotate([135,0,0]) {
                translate([0,0,offset_form_cube]) rotate([0,180,0]) 
                    stepper_holder(show_itr9606);
            }
            difference(){
                r = 10;
                dz = -(offset_form_cube-thinkness)*sqrt(2)+r*sqrt(2);
                // r+1: 防止因计算误差出现缝隙
                translate([0,0,dz-sqrt(2)*(r+1)/2]) rotate([45,0,0])
                    cube([width,r+1,r+1],center=true);
                translate([0,0,dz])
                    rotate([0,90,0]) cylinder(60, r=r, center=true);
            }
        }
        // 4.6mm固定孔
        for(i=[-1:2:1]){
            for(j=[-1:2:1]){
                translate([-12*i,-30*j,-105])
                    cylinder(20,d=m4_hole_through,center=true);
            }
        }
    }
}

base(show_itr9606 = true);


rotate([45,0,0]) rotate([0,0,0]) rubik_cube();

rotate([-135,0,0]) {
    translate([0,0,offset_form_cube]) rotate([0,180,180]) 
    {
        color("grey") stepper42();
        translate([0,0,22]) rotate([0,0,0]) robot_arm(14);
    }
}
rotate([135,0,0]) {
    translate([0,0,offset_form_cube]) rotate([0,180,0])
    {
        color("grey") stepper42();
        translate([0,0,22]) rotate([0,0,20]) robot_arm(0);
    }
}


//translate([width/2,0,-105.7]) #cube([10,50,10],center=true);
